<?php
namespace api\controllers;

use api\common\controllers\BaseController;
use api\models\Token;
use api\models\User;
use Faker\Provider\Uuid;

class UserController extends BaseController{

    public function actionRegister(){
        $data = \Yii::$app->request->post();
        $model = new User();
        $model->setScenario('register');
        static::$FIELDS_KEY = 'register';
        $model->load($data,'');
        if($model->validate()){
            $model->user_id = Uuid::uuid();
            if(!$model::find()->where(['phone'=>$data['phone']])->one()){
                $token = new Token();
                $model->password = $model->generatePasswordHash($data['password']);
                $model->token_id = $token->getTokenId();
                if(!$model->insert(false)){
                    static::$CODE = 1002;
                }
            }else{
                static::$CODE = 1001;
            }

        }
        return $model;
    }

    public function actionUserInfo(){
        $data = \Yii::$app->request->get();
        $model = new User();
        $model->setScenario('user_info');
        static::$FIELDS_KEY = 'user_info';
        $model->load($data,'');
        if($model->validate()){
            $userId = $data['user_id'];
            $model = $model::find()->where(['user_id'=>$userId])->one();
            if($model==null){
                static::$CODE = 1002;
                static::$STATUSTEXT = '没有该用户';
            }
        }
        return $model;
    }

    protected function verbs()
    {
        $verbs = parent::verbs(); // TODO: Change the autogenerated stub
        $verbs['register'] = ['POST'];
        $verbs['user_info'] = ['GET'];
        return $verbs;
    }
}
